package com.alasbusy.alasbusyapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.bean.MusicListBean;
import com.alasbusy.alasbusyapp.utils.PinYingUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/10.
 */

public class MusicListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<MusicListBean> mData = new ArrayList<>();
    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    public static final int LOADING_NO = 2;
    //上拉加载更多状态-默认为0
    private int load_more_status = 0;
    private static final int TYPE_ITEM = 1;  //普通Item View
    private static final int TYPE_FOOTER = 2;  //底部FootView
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private LayoutInflater mInflater;

    public MusicListAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    public void setList(List<MusicListBean> list) {
        mData.clear();
        mData.addAll(list);
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            /*View view = mInflater.inflate(R.layout.visitor_reservation_item, parent, false);
            //这边可以做一些属性设置，甚至事件监听绑定
            //view.setBackgroundColor(Color.RED);
            ItemViewHolder itemViewHolder = new ItemViewHolder(view);
            return itemViewHolder;*/
            ItemViewHolder viewHolder = new ItemViewHolder(mInflater.inflate(R.layout.music_item_view, parent, false));
            return viewHolder;
        } else if (viewType == TYPE_FOOTER) {
            View foot_view = mInflater.inflate(R.layout.listview_footer, parent, false);
            //这边可以做一些属性设置，甚至事件监听绑定
            //view.setBackgroundColor(Color.RED);
            FootViewHolder footViewHolder = new FootViewHolder(foot_view);
            return footViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            MusicListBean musicListBean = mData.get(position);
            viewHolder.setData(musicListBean, position);
        } else {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            switch (load_more_status) {
                case PULLUP_LOAD_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.pull_to_refresh_loadmore_text.setText("上拉加载...");
                    break;
                case LOADING_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.pull_to_refresh_loadmore_text.setText("正在加载...");
                    break;
                case LOADING_NO:
                    footViewHolder.itemView.setVisibility(View.INVISIBLE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size() + 1;
    }

    /**
     * 进行判断是普通Item视图还是FootView视图
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为footerView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    /**
     * //上拉加载更多
     * PULLUP_LOAD_MORE=0;
     * //正在加载中
     * LOADING_MORE=1;
     * //加载完成已经没有更多数据了
     * NO_MORE_DATA=2;
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }

    public int getPositionForSection(String c) {
        for (int i = 0; i < mData.size(); i++) {
            MusicListBean musicListBean = mData.get(i);
            if (PinYingUtils.getSortLetter(musicListBean.getTitle()).equals(c)) {
                return i;
            }
        }
        return 0;
}


public class ItemViewHolder extends RecyclerView.ViewHolder {
    TextView music_title;
    TextView music_anthor;
    ImageView music_img;

    public ItemViewHolder(View itemView) {
        super(itemView);
        music_title = itemView.findViewById(R.id.music_title);
        music_anthor = itemView.findViewById(R.id.music_anthor);
        music_img = itemView.findViewById(R.id.music_img);
    }

    public void setData(MusicListBean data, int position) {
        music_title.setText(data.getTitle());
        music_anthor.setText(data.getAnthor());
        Glide.with(mContext)
                .load(data.getImage())
                //.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存多个尺寸
                .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                //.error(R.mipmap.logo)
                .into(/*new GlideDrawableImageViewTarget(*/music_img)/*) {
                    @Override
                    public void onLoadStarted(Drawable placeholder) {
                        super.onLoadStarted(placeholder);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                    }

                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                        super.onResourceReady(resource, animation);
                    }
                })*/;

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //mContext.startActivity(new Intent(mContext, HtmlCssDetailActivity.class));
            }
        });
    }
}

class FootViewHolder extends RecyclerView.ViewHolder {
    TextView pull_to_refresh_loadmore_text;

    public FootViewHolder(View itemView) {
        super(itemView);
        pull_to_refresh_loadmore_text = (TextView) itemView.findViewById(R.id.pull_to_refresh_loadmore_text);
    }
}

}
